from django.shortcuts import render, redirect
from app01 import models
from django.utils.safestring import mark_safe
# Create your views here.


def business(request):
    v1 = models.Business.objects.all()
    v2 = models.Business.objects.all().values("id", "caption")
    v3 = models.Business.objects.all().values_list("id", "caption")
    return render(request, 'business.html', {'v1': v1, 'v2': v2, 'v3': v3})


def host(request):
    if request.method == "GET":
        h1 = models.Host.objects.all()
        h2 = models.Host.objects.all().values("nid", "hostname", "b_id", "b__caption")
        h3 = models.Host.objects.all().values_list("nid", "hostname", "b_id", "b__caption")
        b_list = models.Business.objects.all()
        return render(request, 'host.html', {'h1': h1, 'h2': h2, 'h3': h3, 'b_list':b_list})

    elif request.method == "POST":
        hostname = request.POST.get('hostname')
        ip = request.POST.get('ip')
        port = request.POST.get('port')
        b_id = request.POST.get('b_id')
        models.Host.objects.create(hostname=hostname,
                                   ip=ip,
                                   port=port,
                                   b_id=b_id)
        return redirect('/host')


def app(request):
    app_list = models.Application.objects.all()
    return render(request, 'app.html', {'app_list': app_list})


li = []
for i in range(150):
    li.append(i)


def user_list(request):
    current_page = request.GET.get('p', 1)
    current_page = int(current_page)
    # page_one_count = 10  # 每页显示的数据条数
    pc = request.COOKIES.get('page_one_count', 10)
    print(pc)
    page_one_count = int(pc)
    print(page_one_count)
    start = (current_page - 1) * page_one_count
    end = (current_page) * page_one_count
    data = li[start:end]

    all_count = len(li)  # 获取总数据条数
    count, y = divmod(all_count, page_one_count)  # 根据每页显示数据条数获取需要有多少页，如果有余数，就加一页
    if y:
        count += 1

    page_list = []  # 翻页创建标签
    page_row = 11  # 可显示的页码数11条
    if count < page_row:  # 如果总的页码数小于可显示的页面数
        start_page = 1
        end_page = count + 1
    else:
        if current_page <= (page_row+1)/2:
            start_page = 1
            end_page = page_row + 1
        else:
            start_page = current_page - (page_row-1)/2
            end_page = current_page + (page_row+1)/2
            if (current_page + (page_row-1)/2) > count:
                end_page = count + 1
                start_page = count - page_row + 1

    if current_page == 1:
        prev_page = '<a class="page" style="display:none" href="javascript:void(0)">[上一页]</a>'
    else:
        prev_page = '<a class="page " href="/user_list/?p=%s">[上一页]</a>' % (current_page-1)
    page_list.append(prev_page)

    for i in range(int(start_page), int(end_page)):
        if i == current_page:
            tmp = '<a class="page active" href="/user_list/?p=%s">[%s]</a>' % (i, i)
        else:
            tmp = '<a class="page " href="/user_list/?p=%s">[%s]</a>' % (i, i)
        page_list.append(tmp)

    if current_page == count:
        next_page = '<a class="page "  style="display:none" href="javascript:void(0)">[下一页]</a>'
    else:
        next_page = '<a class="page " href="/user_list/?p=%s">[下一页]</a>' % (current_page + 1)
    page_list.append(next_page)

    page_str = "".join(page_list)
    page_str = mark_safe(page_str)
    return render(request, 'user_list.html', {'li': data, 'page_str': page_str})

user_info = {
    "alex": {"pwd": "123"},
    "zcl": {"pwd": "123"}
}


def login(request):
    if request.method == "GET":
        return render(request, 'login.html')
    if request.method == "POST":
        u = request.POST.get('username')
        p = request.POST.get('pwd')
        dic = user_info.get(u)
        if not dic:
            return render(request, 'login.html')
        if dic["pwd"] == p:
            res = redirect('/index')
            res.set_cookie("username111", u)
            return res
        else:
            return render(request, 'login.html')


def auth(func):
    def inner(reqeust, *args, **kwargs):
        v = reqeust.COOKIES.get('username111')
        if not v:
            return redirect('/login')
        return func(reqeust, *args, **kwargs)
    return inner


@auth
def index(request):
    c_name = request.COOKIES.get("username111")
    return render(request, 'index.html', {"c_name": c_name})
